<?php

	class Devmod_method_mail extends Devmod_methods_abstraction {
		
		protected $after_finalized = array(array(
										'for_method' => 'save',
										'for_method_type' => 'frontend_add',
										'use' => 'bind_mail'
									),array(
										'for_method' => 'save',
										'for_method_type' => 'add',
										'use' => 'bind_mail'
									));
		
		public function set_config($args)
		{
			$this->update_config('mail', $args[0]);	
		}
		
		public function bind_mail($return)
		{
			if($return === false) return $return;
			
			$args = $this->config[$this->type]['mail'];
			
			$this->view->assign(array(
				'fields' => $this->config[$this->type]['sql_fields'],
				'host' => SiteConfig::Domain
			));
			
			if($this->view->template_dir !== "theme/__admin/tpl/" && !isset($args['tpl']))
			{
				$this->view->template_dir = "share/tpl/"; // its an external theme, so set share/tpl/common folders
			}
			
			$_tpl = 'common/mailer.tpl';
			
			if(isset($args['tpl']))
			{
				$_tpl = $args['tpl'];
			}
			
			$mailer_text = utf8_decode($this->view->show($_tpl));
			
			foreach($args['to'] as $mail => $name)
			{
				sendmail($this->helper->eval_vars($mail, $this->config[$this->type]['sql_fields']), $this->helper->eval_vars($name, $this->config[$this->type]['sql_fields']), $args['subject'], $mailer_text);
			}
			
			return $return;
		}
		
	}